<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Task
 *
 * @author QuangKhoi
 */
class Task {
    public $taskId;
    public $taskName;
    public $taskContent;
    public $taskUserId;
    public $taskAssignedTo;
    public $taskDateCreated;
    public $taskType;
    public $signalId;
    public $status;
    public $signal;
    public $streamId;
    
    public function __constructor() {
        
    }
    
    public static function withId($id) {
        $instance = new self();
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM tasks WHERE taskid = '$id'");
        if ($result->returnCode == 200) {
            if ($result->dataTable->numOfRow == 1) {
                $row = $result->dataTable->rows[0];
                $instance->taskId = $row['taskid'];
                $instance->taskName = $row['taskname'];
                $instance->taskContent = $row['taskcontent'];
                $instance->taskUserId = $row['taskuserid'];
                $instance->taskAssignedTo = $row['taskassignedto'];
                $instance->taskDateCreated = new DateTime($row['taskdatecreated']);
                $instance->taskType = $row['tasktype'];
                $instance->signalId = $row['signalid'];
                $instance->status = $row['status'];
                $instance->signal = Signal::withId($instance->signalId);
                return $instance;
            }
            else {
                $controller = new ErrorController(new Error(), new ErrorView(), 
                    "404", "Not found any user with provided email");
                $controller->returnError();
                return NULL;
            }
        }
        else {
            $controller = new ErrorController(new Error(), new ErrorView, 
                    $result->returnCode, $result->message);
            $controller->returnError();
            return NULL;
        }
    }
    
    /**
     * Construct a task with data
     * @param String $name
     * @param String $content
     * @param Int $userId
     * @param Int $assignedTo
     * @param Date $dateCreated
     * @param Int $type
     * @param Int $signalId
     */
    public static function withData($name, $content, $userId, $assignedTo,
            $dateCreated, $type, $signalId, $streamId) {
        $instance = new self();
        $instance->taskId = "";
        $instance->taskName = $name;
        $instance->taskContent = $content;
        $instance->taskUserId = $userId;
        $instance->taskAssignedTo = $assignedTo;
        $instance->taskDateCreated = $dateCreated;
        $instance->taskType = $type;
        $instance->signalId = $signalId;
        $instance->status = 1;
        return $instance;
    }
    
}

?>
